package com.sdkx.app.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.OpacityBar;
import com.larswerkman.holocolorpicker.SVBar;
import com.sdkx.app.R;
import com.sdkx.app.view.BrokenLineView;

public class LineColorDialog extends Dialog implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    ImageView colorImage;
    BrokenLineView brokenLineView;
    ICallback iCallback;
    SeekBar mSeekBar;
    int lineWidth;
    int oldColor = Color.parseColor("#FF0000");

    public LineColorDialog(@NonNull Context context) {
        super(context);
        initView();
    }

    private void initView() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(getContext().getResources().getColor(android.R.color.transparent)));
        View view = getLayoutInflater().inflate(R.layout.alarm_color_dialog, null);
        setContentView(view);
        view.findViewById(R.id.linecolorshow).setOnClickListener(this);
        mSeekBar = view.findViewById(R.id.seekBar);
        mSeekBar.setOnSeekBarChangeListener(this);
        colorImage = view.findViewById(R.id.linecolorshow);
        brokenLineView = view.findViewById(R.id.lineimage);
        view.findViewById(R.id.save).setOnClickListener(this);
        view.findViewById(R.id.cancel).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linecolorshow:
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("选择颜色");
                View view = LayoutInflater.from(getContext()).inflate(R.layout.color_picker, null);
                final ColorPicker picker = (ColorPicker) view.findViewById(R.id.picker);
                final SVBar svBar = (SVBar) view.findViewById(R.id.svbar);
                final OpacityBar opacityBar = (OpacityBar) view.findViewById(R.id.opacitybar);
                picker.addSVBar(svBar);
                picker.addOpacityBar(opacityBar);
                picker.setShowOldCenterColor(false);
                if (oldColor != -1)
                    picker.setColor(oldColor);
                builder.setView(view);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        int argb = picker.getColor();
                        oldColor = argb;
                        colorImage.setColorFilter(argb);
                        brokenLineView.setLineColor(argb);
                        brokenLineView.postInvalidate();

                    }
                });
                Dialog dialog = builder.create();
                dialog.show();
                break;
            case R.id.save:
                if(iCallback != null){
                    iCallback.onCallBack(oldColor,lineWidth >0 ? lineWidth : 1,BrokenLineView.getBitmap(100,100,lineWidth,oldColor));
                }
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }

    public void setColor(int color) {
        oldColor = color;
        colorImage.setColorFilter(color);
        brokenLineView.setLineColor(color);
    }
    public void setLineWidth(int width){
        this.lineWidth = width;
        brokenLineView.setLineWidth(width);
        mSeekBar.setProgress(width);
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        lineWidth = progress;
        brokenLineView.setLineWidth(progress);
        brokenLineView.postInvalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
    public void setColorCallback(ICallback colorCallback){
        iCallback = colorCallback;
    }
    public interface  ICallback{
        void onCallBack(int color,int width,Bitmap bitmap);
    }

}
